import org.junit.Test;

import java.io.*;
import java.util.regex.Pattern;

public class PunchTableTest {
    private static final String outFilePath = ".\\output\\打卡.txt";
    private static final String eFilePath = ".\\output\\异常.txt";
    private static final String filename = ".\\input\\员工打卡流水.txt";

    @Test
    public void testPunchTable() {
        System.out.println("进阶1:" + (ex1() ? "完成" : "未完成"));
        System.out.println("进阶2:" + (ex2() ? "完成" : "未完成"));
        System.out.println("进阶3:" + (ex3() ? "完成" : "未完成"));
        System.out.println("进阶4:" + (ex4() ? "完成" : "未完成"));
        System.out.println("进阶5:" + (ex5() ? "完成" : "未完成"));
        System.out.println("进阶6:" + (ex6() ? "完成" : "未完成"));
        System.out.println("进阶7:" + (ex7() ? "完成" : "未完成"));

    }

    /*
     * @Description 进阶1：按照程序输出后利用正则进行内容校验
     * @param
     * @return boolean
     * @author 阿鸣
     * @date 2022/3/18 19:09
     * @see
     */
    public boolean ex1() {
        // 打卡表
        PunchTable pt = new PunchTable();
        // 读取文件

        File file = new File(filename);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                pt.addPunchByString(tempString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


        pt.writeToFile(outFilePath, eFilePath, "GBK");
        // outFile是程序刚输出的文件
        File outFile = new File(outFilePath);
        // 进行校验
        boolean flg = true;
        String pattern = "\\d+\\|[\\u4E00-\\u9FA5]+[ ]*?\\|([0][0-9]|[1][0-9]|[2][1-3]):[0-5][0-9]:[0-5][0-9]\\|([0][0-9]|[1][0-9]|[2][1-3]):[0-5][0-9]:[0-5][0-9]";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(outFile), "GBK"));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                // 出现一个错误就为false
                flg &= Pattern.matches(pattern, tempString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return flg;
    }

    public boolean ex2() {
        // 打卡表
        PunchTable pt = new PunchTable();
        // 读取文件
        File file = new File(filename);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                pt.addPunchByString(tempString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 输出并指定GBK

        pt.writeToFile(outFilePath, eFilePath, "GBK");
        return true;
    }

    public boolean ex3() {
        return true;
    }

    public boolean ex4() {
        // 打卡表
        PunchTable pt = new PunchTable();
        // 读取文件
        File file = new File(filename);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                pt.addPunchByString(tempString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 输出并指定GBK
        pt.writeToFile(outFilePath, eFilePath, "UTF-8");
        return true;
    }

    public boolean ex5() {
        // TODO:配置Maven文件，导入FastJson包
        return false;
    }

    public boolean ex6() {
        // 打卡表
        PunchTable pt = new PunchTable();
        // 读取文件
        File file = new File(filename);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                pt.addPunchByString(tempString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean ex7() {
        // TODO:学习一下java并发编程
        return false;
    }
}
